package com.zb.controller.settings;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.zb.common.annotation.Permission;
import com.zb.common.annotation.WriteLog;
import com.zb.common.exception.BusinessException;
import com.zb.common.po.enums.ResponseCode;
import com.zb.common.po.model.CodeTable;
import com.zb.common.po.query.CodeTableQuery;
import com.zb.common.po.vo.AjaxResponse;
import com.zb.common.utils.PaginationResult;
import com.zb.service.CodeTableService;

@Controller
@RequestMapping(value = "/settings")
public class TableCodeController {

    Logger logger = LoggerFactory.getLogger(TableCodeController.class);

    @Resource
    private CodeTableService codeTableService;

    @Permission(code = "codetable:list")
    @RequestMapping(value = "/codetable_list.do")
    public String codetable_list(Model model) {
        return "page/settings/codetable_list";
    }

    @Permission(code = "codetable:list")
    @ResponseBody
    @RequestMapping(value = "/loadCodetableList.do")
    public AjaxResponse<PaginationResult<CodeTable>> loadCodetableList(
            HttpSession session, CodeTableQuery query) {
        AjaxResponse<PaginationResult<CodeTable>> response = new AjaxResponse<PaginationResult<CodeTable>>();
        try {
            query.setPid(0);
            PaginationResult<CodeTable> result = codeTableService
                    .findCodeTableList4Page(query);
            response.setData(result);
            response.setResponseCode(ResponseCode.SUCCESS);
        } catch (Exception e) {
            logger.error("查询码表失败：{}", e);
            response.setResponseCode(ResponseCode.SERVERERROR);
            response.setErrorMsg("查询码表失败");
        }
        return response;
    }

    @Permission(code = "codetable:add")
    @ResponseBody
    @RequestMapping(value = "/codetable_add.do")
    @WriteLog(opObject = "码表", opDescription = "新增码表：#{#param0}", paramIndexes = { 1 })
    public AjaxResponse<?> codetableAdd(HttpSession session, CodeTable codeTable) {
        AjaxResponse<?> response = new AjaxResponse<Object>();
        try {
            codeTableService.addCodeTable(codeTable);
            response.setResponseCode(ResponseCode.SUCCESS);
        } catch (BusinessException e) {
            logger.error("新增码表失败：{}", e);
            response.setResponseCode(ResponseCode.BUSINESSERROR);
            response.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            logger.error("新增码表失败：{}", e);
            response.setResponseCode(ResponseCode.SERVERERROR);
            response.setErrorMsg("新增码表失败");
        }
        return response;
    }

    @Permission(code = "codetable:update")
    @ResponseBody
    @RequestMapping(value = "/codetable_update.do")
    @WriteLog(opObject = "码表", opDescription = "修改码表：#{#param0}", paramIndexes = { 1 })
    public AjaxResponse<?> codetableUpdate(HttpSession session,
            CodeTable codeTable) {
        AjaxResponse<?> response = new AjaxResponse<Object>();
        try {
            codeTableService.updateCodeTable(codeTable);
            response.setResponseCode(ResponseCode.SUCCESS);
        } catch (BusinessException e) {
            logger.error("修改码表失败：{}", e);
            response.setResponseCode(ResponseCode.BUSINESSERROR);
            response.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            logger.error("修改码表失败：{}", e);
            response.setResponseCode(ResponseCode.SERVERERROR);
            response.setErrorMsg("修改码表失败");
        }
        return response;
    }

    @Permission(code = "codetable:list")
    @RequestMapping(value = "/codetable_sub_list.do")
    public ModelAndView codetableSubList(Model model, Integer id) {
        ModelAndView view = new ModelAndView("page/settings/codetable_sub_list");
        CodeTable codeTable = codeTableService.getCodeTable(id);
        view.addObject("codeTable", codeTable);
        view.addObject("id", id);
        return view;
    }

    @Permission(code = "codetable:list")
    @ResponseBody
    @RequestMapping(value = "/loadCodetableSubList.do")
    public AjaxResponse<PaginationResult<CodeTable>> loadCodetableSubList(
            HttpSession session, CodeTableQuery query) {
        AjaxResponse<PaginationResult<CodeTable>> response = new AjaxResponse<PaginationResult<CodeTable>>();
        try {
            PaginationResult<CodeTable> result = codeTableService
                    .findCodeTableList4Page(query);
            response.setData(result);
            response.setResponseCode(ResponseCode.SUCCESS);
        } catch (Exception e) {
            logger.error("查询码表失败：{}", e);
            response.setResponseCode(ResponseCode.SERVERERROR);
            response.setErrorMsg("查询码表失败");
        }
        return response;
    }
}
